package com.maddox.maddoxppjoke.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.maddox.maddoxppjoke.R;
import com.maddox.maddoxppjoke.databinding.LayoutFeedTypeImageBinding;
import com.maddox.maddoxppjoke.databinding.LayoutFeedTypeVideoBinding;
import com.maddox.maddoxppjoke.model.Feed;

public class FeedAdapter extends PagedListAdapter<Feed, FeedAdapter.ViewHolder> {
    //用于视频的播放暂停页面控制绑定
    private String mCategory;
    private Context mContext;
    private LayoutInflater inflater;


    protected FeedAdapter(Context context, String category) {
        super(new DiffUtil.ItemCallback<Feed>() {
            @Override
            public boolean areItemsTheSame(@NonNull Feed oldItem, @NonNull Feed newItem) {
                return oldItem.id == newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull Feed oldItem, @NonNull Feed newItem) {
                return oldItem.equals(newItem);
            }
        });
        inflater = LayoutInflater.from(context);
        mContext = context;
        mCategory = category;
    }


    @Override
    public int getItemViewType(int position) {
        Feed feed = getItem(position);
        return feed.itemType;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = null;
        if (viewType == Feed.TYPE_IMAGE) {

            binding = DataBindingUtil.inflate(inflater, R.layout.layout_feed_type_image, parent, false);

        } else {
            binding = DataBindingUtil.inflate(inflater, R.layout.layout_feed_type_video, parent, false);

        }

        return new ViewHolder(binding.getRoot(), binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bindData(getItem(position));
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding dataBinding;

        public ViewHolder(@NonNull View itemView, ViewDataBinding binding) {
            super(itemView);
            dataBinding = binding;
        }

        //针对图片或者视频进行绑定
        public void bindData(Feed feed) {
            if (dataBinding instanceof LayoutFeedTypeImageBinding) {
                LayoutFeedTypeImageBinding imageBinding = (LayoutFeedTypeImageBinding) dataBinding;
                imageBinding.setFeed(feed);
                imageBinding.feedImage.bindData(feed.width, feed.height, 16, feed.cover);
            } else {
                LayoutFeedTypeVideoBinding videoBinding = (LayoutFeedTypeVideoBinding) dataBinding;
                videoBinding.setFeed(feed);
                videoBinding.feedVideo.bindData(mCategory, feed.width, feed.height, feed.cover, feed.url);
            }
        }


    }
}
